package com.naizhuai.util;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import com.naizhuai.ui.entity.CommonLabel;

import java.util.List;

/**
 * 金钱处理，效果金额等
 * Created by code_han on 2018/1/29.
 */

public class MathUtils {

    /**
     * 对于金钱的处理
     *
     * @param moneyNumber 金钱数
     * @return
     */
    public static String getMoneyChange(String moneyNumber) {
        double money = parseStringToDouble(moneyNumber);
        if (money < 0)
            return "";
        if (money < 1000)
            return String.valueOf(getNumberPointChanged(money, 2, true)) + "元";
        if (money < 100000) {
            int thN = (int) (money / 1000);
            double smN = money % 1000;
            return String.valueOf(thN) + "," + String.valueOf(getNumberPointChanged(smN, 2, false)) + "元";
        }
        if (money < 10000000)
            return String.valueOf(getNumberPointChanged(money / 10000, 2, true)) + "万元";
        if (money < 100000000) {
            double number = money / 10000;
            int thN = (int) (number / 1000);
            double smN = number % 1000;
            return String.valueOf(thN) + "," + String.valueOf(getNumberPointChanged(smN, 2, false)) + "万元";
        } else {
            return String.valueOf(getNumberPointChanged(money / 100000000, 2, true)) + "亿元";
        }
    }

    public static double parseStringToDouble(String str) {
        if (TextUtils.isEmpty(str))
            return -1;
        try {
            return Double.valueOf(str);
        } catch (Exception e) {
            return -1;
        }
    }

    public static float parseStringToFloat(String str) {
        if (TextUtils.isEmpty(str))
            return -1;
        try {
            return Float.valueOf(str);
        } catch (Exception e) {
            return -1;
        }
    }

    public static double canParseStringToDouble(String... strs) {
        double number = -1;
        for (String str : strs) {
            if (TextUtils.isEmpty(str))
                return -1;
            try {
                number = Double.valueOf(str);
            } catch (Exception e) {
                return -1;
            }
        }
        return number;
    }

    /**
     * 两个数相减
     *
     * @param numberA
     * @param numberB
     * @return
     */
    public static String numberSubstration(String numberA, String numberB) {
        return String.valueOf((int) (parseStringToDouble(numberA) - parseStringToDouble(numberB)));
    }

    /**
     * 两个数相减数返回
     *
     * @param numberA
     * @param numberB
     * @return
     */
    public static double numberSubstrationReNUmber(String numberA, String numberB) {
        return parseStringToDouble(numberA) - parseStringToDouble(numberB);
    }

    /**
     * 两个数相减数返回
     *
     * @param numberA
     * @param numberB
     * @return
     */
    public static String numberRadius(String numberA, String numberB) {
        double bValue = parseStringToDouble(numberA);
        double aValue = parseStringToDouble(numberB);
        if (bValue == 0 || bValue == -1 || aValue == -1)
            return null;
        return getNumberPointChanged((aValue - bValue) / bValue * 100d, 2, true);
    }


    /**
     * 保留index位小数（四舍五入）
     *
     * @param number     需要处理的数据
     * @param index      保留的位数
     * @param isMaxPoint 是不是最高位
     * @return
     */
    @SuppressLint("DefaultLocale")
    private static String getNumberPointChanged(double number, double index, boolean isMaxPoint) {
        double powerIndex = Math.pow(10, index);
        double data = Math.round(number * powerIndex) / powerIndex;
        String span;
        if (isMaxPoint)
            span = "";
        else
            span = "0";
        if (data < 10)
            return String.format(span + span + "%.2f", Math.round(number * powerIndex) / powerIndex);
        if (data < 100)
            return String.format(span + "%.2f", Math.round(number * powerIndex) / powerIndex);
        else
            return String.format("%.2f", Math.round(number * powerIndex) / powerIndex);
    }

    /**
     * 计算改善比例以及效果金额
     *
     * @param preValue   改善前的值
     * @param afterValue 改善后的值
     * @param beConvert  被转换单位参考值
     * @param convert    转换成的单位参考值
     * @return 返回【0】改善比例（%），返回【1】效果金额，返回如果是-1，则数字非法
     */
    public static String[] computeUnitValue(String preValue, String afterValue, String beConvert, String convert) {
        if (canParseStringToDouble(preValue, afterValue, beConvert, convert) == -1) {
            return new String[]{"-1"};
        }
        double preData = parseStringToDouble(preValue);
        double afterData = parseStringToDouble(afterValue);
        double beConvertData = parseStringToDouble(beConvert);
        double ConvertData = parseStringToDouble(convert);
        double ratioData = beConvertData / ConvertData;
        String change = getNumberPointChanged((afterData - preData) / preData * 100, 2, true);
        String moneyEffect = getMoneyChange((afterData - preData) / ratioData + "");
        return new String[]{change + "%", moneyEffect};
    }

    public static String getNumberAddNumber(String sourceNumber,int addNumber){
        return String.valueOf(Integer.valueOf(sourceNumber)+addNumber);
    }


    public static int getCyclerSize(List<CommonLabel> labels, int size) {
        if (labels.size() < 1)
            return size;
        size++;
        int totalSize=size;
        for (CommonLabel label : labels) {
            totalSize = Math.max(totalSize, getCyclerSize(label.msubList, size));
        }
        return totalSize;
    }

}
